﻿namespace NServiceBus.AcceptanceTests.Core.Routing;

using System.Threading.Tasks;
using AcceptanceTesting;
using EndpointTemplates;
using AcceptanceTesting.Customization;
using NUnit.Framework;

public class When_routing_interface_message : NServiceBusAcceptanceTest
{
    [Test]
    public async Task Should_use_interface_types_route()
    {
        var context = await Scenario.Define<Context>()
            .WithEndpoint<Endpoint>(c => c.When(b => b.SendLocal(new StartMessage())))
            .Done(c => c.GotTheMessage)
            .Run();

        Assert.That(context.GotTheMessage, Is.True);
    }

    public class Context : ScenarioContext
    {
        public bool GotTheMessage { get; set; }
    }

    public class Endpoint : EndpointConfigurationBuilder
    {
        public Endpoint()
        {
            EndpointSetup<DefaultServer>((c, r) =>
            {
                c.ConfigureRouting().RouteToEndpoint(typeof(IMyMessage), typeof(Endpoint));
            });
        }

        public class StartMessageHandler : IHandleMessages<StartMessage>
        {
            public Task Handle(StartMessage message, IMessageHandlerContext context)
            {
                return context.Send<IMyMessage>(_ => { });
            }
        }

        public class MyMessageHandler : IHandleMessages<IMyMessage>
        {
            public MyMessageHandler(Context testContext)
            {
                this.testContext = testContext;
            }

            public Task Handle(IMyMessage message, IMessageHandlerContext context)
            {
                testContext.GotTheMessage = true;

                return Task.CompletedTask;
            }

            Context testContext;
        }
    }

    public class StartMessage : IMessage
    {
    }

    public interface IMyMessage : IMessage
    {
    }
}
